home *** CD-ROM | disk | FTP | other *** search
/ isnet Internet / Isnet Internet CD.iso / prog / hiz / 09 / 09.exe / adynware.exe / perl / lib / FindBin.pm < prev    next >
Encoding:
Perl POD Document  |  1999-12-28  |  3.8 KB  |  172 lines

  1.  
  2. =head1 NAME
  3.  
  4. FindBin - Locate directory of original perl script
  5.  
  6. =head1 SYNOPSIS
  7.  
  8.  use FindBin;
  9.  use lib "$FindBin::Bin/../lib";
  10.  
  11.  or
  12.  
  13.  use FindBin qw($Bin);
  14.  use lib "$Bin/../lib";
  15.  
  16. =head1 DESCRIPTION
  17.  
  18. Locates the full path to the script bin directory to allow the use
  19. of paths relative to the bin directory.
  20.  
  21. This allows a user to setup a directory tree for some software with
  22. directories E<lt>rootE<gt>/bin and E<lt>rootE<gt>/lib and then the above example will allow
  23. the use of modules in the lib directory without knowing where the software
  24. tree is installed.
  25.  
  26. If perl is invoked using the B<-e> option or the perl script is read from
  27. C<STDIN> then FindBin sets both C<$Bin> and C<$RealBin> to the current
  28. directory.
  29.  
  30. =head1 EXPORTABLE VARIABLES
  31.  
  32.  $Bin         - path to bin directory from where script was invoked
  33.  $Script      - basename of script from which perl was invoked
  34.  $RealBin     - $Bin with all links resolved
  35.  $RealScript  - $Script with all links resolved
  36.  
  37. =head1 KNOWN BUGS
  38.  
  39. if perl is invoked as
  40.  
  41.    perl filename
  42.  
  43. and I<filename> does not have executable rights and a program called I<filename>
  44. exists in the users C<$ENV{PATH}> which satisfies both B<-x> and B<-T> then FindBin
  45. assumes that it was invoked via the C<$ENV{PATH}>.
  46.  
  47. Workaround is to invoke perl as
  48.  
  49.  perl ./filename
  50.  
  51. =head1 AUTHORS
  52.  
  53. Graham Barr E<lt>F<bodg@tiuk.ti.com>E<gt>
  54. Nick Ing-Simmons E<lt>F<nik@tiuk.ti.com>E<gt>
  55.  
  56. =head1 COPYRIGHT
  57.  
  58. Copyright (c) 1995 Graham Barr & Nick Ing-Simmons. All rights reserved.
  59. This program is free software; you can redistribute it and/or modify it
  60. under the same terms as Perl itself.
  61.  
  62. =head1 REVISION
  63.  
  64. $Revision: 1.4 $
  65.  
  66. =cut
  67.  
  68. package FindBin;
  69. use Carp;
  70. require 5.000;
  71. require Exporter;
  72. use Cwd qw(getcwd abs_path);
  73. use Config;
  74. use File::Basename;
  75.  
  76. @EXPORT_OK = qw($Bin $Script $RealBin $RealScript $Dir $RealDir);
  77. %EXPORT_TAGS = (ALL => [qw($Bin $Script $RealBin $RealScript $Dir $RealDir)]);
  78. @ISA = qw(Exporter);
  79.  
  80. $VERSION = $VERSION = sprintf("%d.%02d", q$Revision: 1.4 $ =~ /(\d+)\.(\d+)/);
  81.  
  82. sub is_abs_path
  83. {
  84.  local $_ = shift if (@_);
  85.  if ($^O eq 'MSWin32')
  86.   {
  87.    return m#^[a-z]:[\\/]#i;
  88.   }
  89.  elsif ($^O eq 'VMS')
  90.   {
  91.     $_ = $ENV{$_} while /^[\w\$\-]+$/ and $ENV{$_};
  92.     return m!^/! or m![<\[][^.\-\]>]! or /:[^<\[]/;
  93.   }
  94.  else
  95.   {
  96.    return m#^/#;
  97.   }
  98. }
  99.  
  100. BEGIN
  101. {
  102.  *Dir = \$Bin;
  103.  *RealDir = \$RealBin;
  104.  
  105.  if($0 eq '-e' || $0 eq '-')
  106.   {
  107.  
  108.    $Script = $RealScript = $0;
  109.    $Bin    = $RealBin    = getcwd();
  110.   }
  111.  else
  112.   {
  113.    my $script = $0;
  114.  
  115.    if ($^O eq 'VMS')
  116.     {
  117.      ($Bin,$Script) = VMS::Filespec::rmsexpand($0) =~ /(.*\])(.*)/;
  118.      ($RealBin,$RealScript) = ($Bin,$Script);
  119.     }
  120.    else
  121.     {
  122.      my $IsWin32 = $^O eq 'MSWin32';
  123.      unless(($script =~ m#/# || ($IsWin32 && $script =~ m#\\#))
  124.             && -f $script)
  125.       {
  126.        my $dir;
  127.        my $pathvar = ($IsWin32) ? 'Path' : 'PATH';
  128.  
  129.        foreach $dir (split(/$Config{'path_sep'}/,$ENV{$pathvar}))
  130.     {
  131.     if(-r "$dir/$script" && (!$IsWin32 || -x _))
  132.          {
  133.           $script = "$dir/$script";
  134.  
  135.       if (-f $0)
  136.            {
  137.  
  138.             $script = $0 unless(-T $script);
  139.            }
  140.           last;
  141.          }
  142.        }
  143.      }
  144.  
  145.      croak("Cannot find current script '$0'") unless(-f $script);
  146.  
  147.  
  148.      $script = getcwd() . "/" . $script unless is_abs_path($script);
  149.  
  150.      ($Script,$Bin) = fileparse($script);
  151.  
  152.      while(1)
  153.       {
  154.        my $linktext = readlink($script);
  155.  
  156.        ($RealScript,$RealBin) = fileparse($script);
  157.        last unless defined $linktext;
  158.  
  159.        $script = (is_abs_path($linktext))
  160.                   ? $linktext
  161.                   : $RealBin . "/" . $linktext;
  162.       }
  163.  
  164.      $Bin     = abs_path($Bin)     if($Bin);
  165.      $RealBin = abs_path($RealBin) if($RealBin);
  166.     }
  167.   }
  168. }
  169.  
  170. 1; # Keep require happy
  171.  
  172.